﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtility;
using System.Data;
using CUREEntity;
using CUREDataAccessLayers.Common;
namespace CUREDataAccessLayers
{
    public class TeamDal
    {
        private common comm = new common();

        /// <summary>
        /// 添加班别数据
        /// </summary>
        /// <param name="id">班别id</param>
        /// <param name="name">班别名称</param>
        /// <param name="time">时间</param>
        /// <returns>返回受影响的行数</returns>
        public int InsertTeam(int id, string name, string time)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(time))
                {
                    commandText.Append("insert into team(team_id,team_name,team_time)values('" + id + "','" + name.Trim() + "','" + time.Trim() + "')");
                    result = DbHelperOra.ExecuteSql(commandText.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 根据班别id更新数据
        /// </summary>
        /// <param name="id">班别id</param>
        /// <param name="name">班别名称</param>
        /// <param name="time">时间</param>
        /// <returns>返回受影响的行数</returns>
        public int UpdateTeamById(string id, string name, string time)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(time))
                {
                    commandText.Append("update team set team_name='" + name.Trim() + "',team_time='" + time.Trim() + "' where team_id='" + id.Trim() + "'");
                    result = DbHelperOra.ExecuteSql(commandText.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 根据id删除班别数据
        /// </summary>
        /// <param name="id">班别id</param>
        /// <returns>返回受影响的行数</returns>
        public int DeleteTeamById(string id)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                if (!string.IsNullOrEmpty(id))
                {
                    commandText.Append("delete from team where team_id='" + id.Trim() + "'");
                    result = DbHelperOra.ExecuteSql(commandText.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 获取team表最大的team_id
        /// </summary>
        /// <returns>返回最大的team_id</returns>
        public decimal GetMaxTeamId()
        {
            decimal maxId = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select max(team_id) from team t");
                maxId = (decimal)DbHelperOra.GetMaxID("team_id", "team");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return maxId;
        }
        /// <summary>
        /// 班组信息分页
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="where">字典</param>
        /// <param name="pageIndex">当前页的索引</param>
        /// <param name="pageSize">每页显示的条数</param>
        /// <param name="totalRecordCount">总记录数</param>
        /// <param name="totalPageCount">总页数</param>
        /// <returns></returns>
        public DataSet GetPagedData<T>(IDictionary<string, object> where, int pageIndex, int pageSize, out int totalRecordCount, out int totalPageCount)
        {
            //IList<T> resultList = null;
            StringBuilder commandText = new StringBuilder();
            DataSet ds = new DataSet();
            try
            {
                //commandText.Append("select t.team_id as ID,t.team_name as Team_Name,t.team_time as Team_Time from team t where 1=1");
                if (where["name"]!=null&&where["name"].ToString() != "")
                {
                    commandText.Append(" and team_name like'%" + where["name"].ToString().Trim() + "%'");
                }
                ds = comm.GetList("team", pageSize, pageIndex, commandText.ToString(), "team_id", out totalRecordCount, out totalPageCount);
                //resultList = this.DbHelperInstance.GetPagedData<T>(pageIndex, pageSize, commandText, "ID", out  totalRecordCount, out  totalPageCount);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }
        /// <summary>
        /// 根据班别id获取班别信息
        /// </summary>
        /// <param name="id">班别id</param>
        /// <returns>返回表</returns>
        public DataTable GetTeamInfoById(string id)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select t.team_id,t.team_name,t.team_time from team t where t.team_id='" + id + "'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
               // this.DbHelperInstance.Fill(dt, commandText);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据名称获取班组信息
        /// </summary>
        /// <param name="name">班组名称</param>
        /// <returns>返回表</returns>
        public DataTable GetTeamByName(string name)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select * from team t where t.team_name='" + name.Trim() + "'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据id和name获取班组信息
        /// </summary>
        /// <param name="id">班组id</param>
        /// <param name="name">班组名称</param>
        /// <returns>返回表</returns>
        public DataTable GetTeamByIdAndName(string id, string name)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select * from team t where t.team_id!='"+id.Trim()+"' and t.team_name='"+name.Trim()+"'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
